/**
 * 
 */
package com.tianxiao.test.web.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tianxiao.test.model.ConflictCheck;
import com.tianxiao.test.result.ConflictResult;
import com.tianxiao.test.service.ConflictCheckService;
import com.tianxiao.test.util.DateUtil;

/**
 * @author leiruiqi
 *
 */

@Controller
public class ConflictCheckController {
	
	public static int[] courseIds = {1,2,3,4,5,6,7,8,9,10,11,12}; 
	
	public static int[] classroomIds = {1,2,3}; 
	
	public static int[] teacherIds = {1,2,3,4,5,6}; 
	
	@Resource
	private ConflictCheckService conflictCheckService;
	
	
	/*@RequestMapping("showAllTimeTable.json")
	@ResponseBody
	public List<ConflictCheck> showAllTimeTable(ConflictCheck conflictCheck){
		ConflictResult<List<ConflictCheck>> result = conflictCheckService.queryByConditonRange(conflictCheck);
		return result.getResult();
	}*/
	@RequestMapping("showAllTimeTable")
	public void showAllTimeTable(Model model,ConflictCheck conflictCheck,String rangeTimeStart,String rangeTimeEnd){
		
		addBaseInfo(model);
		if(rangeTimeStart==null ||rangeTimeEnd==null){
			model.addAttribute("result", new ConflictResult<Object>());
			return ;
		}
		Date startTime = getDateForStartTime(rangeTimeStart);
		Date endTime = getDateForEndTime(rangeTimeEnd);
		if(startTime == null ||endTime == null){
			model.addAttribute("result", new ConflictResult<Object>());
			return ;
		}
		conflictCheck.setRangeStart(startTime.getTime());
		conflictCheck.setRangeEnd(endTime.getTime());
		ConflictResult<List<ConflictCheck>> result = conflictCheckService.queryByConditonRange(conflictCheck);
		model.addAttribute("result", result.getResult());
		return;
	}
	@RequestMapping("showAllTime")
	@ResponseBody
	public ConflictResult<List<ConflictCheck>> showAllTimeTableJson(Model model,ConflictCheck conflictCheck,String rangeTimeStart,String rangeTimeEnd){
		
		//addBaseInfo(model);
		if(rangeTimeStart==null ||rangeTimeEnd==null){
			return  new ConflictResult<List<ConflictCheck>>();
		}
		Date startTime = getDateForStartTime(rangeTimeStart);
		Date endTime = getDateForEndTime(rangeTimeEnd);
		if(startTime == null ||endTime == null){
			return  new ConflictResult<List<ConflictCheck>>();
		}
		conflictCheck.setRangeStart(startTime.getTime());
		conflictCheck.setRangeEnd(endTime.getTime());
		ConflictResult<List<ConflictCheck>> result = conflictCheckService.queryByConditonRange(conflictCheck);
		return result;
	}
	
	private void addBaseInfo(Model model){
		model.addAttribute("courseIds", courseIds);
		model.addAttribute("classroomIds", classroomIds);
		model.addAttribute("teacherIds", teacherIds);
	}
	
	private Date getDateForStartTime(String dataStr){
		try {
			return DateUtil.parseStandardDate(dataStr);
			//DateUtils.p
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	private Date getDateForEndTime(String dataStr){
		try {
			return DateUtil.getTheDayEndTime(DateUtil.parseStandardDate(dataStr));
			//DateUtils.p
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	private Date getDateTime(String dataTimeStr){
		try {
			return DateUtil.parseDateTime(dataTimeStr);
			//DateUtils.p
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping("save")
	@ResponseBody
	public ConflictResult<List<ConflictCheck>> save(ConflictCheck conflictCheck,String startTimeStr,String endTimeStr){
		if(startTimeStr==null ||endTimeStr==null){
			return  new ConflictResult<List<ConflictCheck>>();
		}
		if(conflictCheck.getCourseId()==null ||conflictCheck.getClassroomId()==null ||conflictCheck.getTeacherId() ==null){
			return  new ConflictResult<List<ConflictCheck>>();
		}
		Date startTime = getDateTime(startTimeStr);
		Date endTime = getDateTime(endTimeStr);
		if(startTime == null ||endTime == null){
			return  new ConflictResult<List<ConflictCheck>>();
		}
		conflictCheck.setStartTime(startTime.getTime());
		conflictCheck.setEndTime(endTime.getTime());
		ConflictResult<List<ConflictCheck>> result = conflictCheckService.save(conflictCheck);
		return result;
	}
	
	
	
	
}
